package com.gzxw.mogublog.xo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzxw.mogublog.commons.entity.SysDicData;
import com.gzxw.mogublog.commons.entity.SysDicType;
import com.gzxw.mogublog.utils.StringUtils;
import com.gzxw.mogublog.xo.mapper.SysDicTypeMapper;
import com.gzxw.mogublog.xo.service.SysDicDataService;
import com.gzxw.mogublog.xo.service.SysDicTypeService;
import com.gzxw.mogublog.xo.vo.SysDictTypeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Service
public class SysDicTypeServiceImpl extends ServiceImpl<SysDicTypeMapper, SysDicType> implements SysDicTypeService {

    @Autowired
    private SysDicDataService sysDicDataService;

    @Override
    public IPage<SysDicType> getList(SysDictTypeVO sysDictTypeVO) {

        QueryWrapper<SysDicType> queryWrapper = new QueryWrapper<>();
        // 字典名称
        if (StringUtils.isNotEmpty(sysDictTypeVO.getDictName()) && !StringUtils.isEmpty(sysDictTypeVO.getDictName().trim())) {
            queryWrapper.like("dict_name", sysDictTypeVO.getDictName().trim());
        }

        // 字典类型
        if (StringUtils.isNotEmpty(sysDictTypeVO.getDictType()) && !StringUtils.isEmpty(sysDictTypeVO.getDictType().trim())) {
            queryWrapper.like("dict_type", sysDictTypeVO.getDictType().trim());
        }

        if (StringUtils.isNotEmpty(sysDictTypeVO.getOrderByAscColumn())) {
            // 将驼峰转换成下划线
            String column = StringUtils.underLine(new StringBuffer(sysDictTypeVO.getOrderByAscColumn())).toString();
            queryWrapper.orderByAsc(column);
        } else if (StringUtils.isNotEmpty(sysDictTypeVO.getOrderByDescColumn())) {
            // 将驼峰转换成下划线
            String column = StringUtils.underLine(new StringBuffer(sysDictTypeVO.getOrderByDescColumn())).toString();
            queryWrapper.orderByDesc(column);
        } else {
            queryWrapper.orderByDesc("sort", "create_time");
        }
        Page<SysDicType> page = new Page<>();
        page.setCurrent(sysDictTypeVO.getCurrentPage());
        page.setSize(sysDictTypeVO.getPageSize());
        queryWrapper.eq("status", 1);
        return this.page(page, queryWrapper);
    }

    @Override
    public String addSysDictType(SysDictTypeVO sysDictTypeVO) {

        // 判断当前字典类型是否存在
        QueryWrapper<SysDicType> sysDicTypeQueryWrapper = new QueryWrapper<>();
        sysDicTypeQueryWrapper.eq("dict_type", sysDictTypeVO.getDictType());
        sysDicTypeQueryWrapper.eq("status", 1);
        sysDicTypeQueryWrapper.last("limit 1");
        SysDicType temp = this.getOne(sysDicTypeQueryWrapper);
        if (temp != null) {
            return "该分类已经存在";
        }
        SysDicType sysDicType = new SysDicType();
        sysDicType.setDictName(sysDictTypeVO.getDictName());
        sysDicType.setDictType(sysDictTypeVO.getDictType());
        sysDicType.setRemark(sysDictTypeVO.getRemark());
        sysDicType.setIsPublish(sysDictTypeVO.getIsPublish());
        sysDicType.setSort(sysDictTypeVO.getSort());
        sysDicType.insert();
        return "插入成功";
    }

    @Override
    public String editSysDictType(SysDictTypeVO sysDictTypeVO) {
        // 判断编辑的字典是否存在
        SysDicType sysDictType = this.getById(sysDictTypeVO.getUid());
        if (!sysDictType.getDictType().equals(sysDictTypeVO.getDictType())) {
            QueryWrapper<SysDicType> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("dict_type", sysDictTypeVO.getDictType());
            queryWrapper.eq("status", 1);
            queryWrapper.last("limit 1");
            SysDicType temp = this.getOne(queryWrapper);
            if (temp != null) {
                return "该字典类型已经存在";
            }
        }
        sysDictType.setDictName(sysDictTypeVO.getDictName());
        sysDictType.setDictType(sysDictTypeVO.getDictType());
        sysDictType.setRemark(sysDictTypeVO.getRemark());
        sysDictType.setIsPublish(sysDictTypeVO.getIsPublish());
        sysDictType.setSort(sysDictTypeVO.getSort());
//        sysDictType.setUpdateByUid(request.getAttribute(SysConf.ADMIN_UID).toString());
        sysDictType.setUpdateTime(new Date());
        sysDictType.updateById();
        // 获取Redis中特定前缀
        return "编辑成功";
    }

    @Override
    public String deleteBatchSysDictType(List<SysDictTypeVO> sysDictTypeVoList) {
        if (sysDictTypeVoList.size() <= 0) {
            return "参数有误";
        }
        List<String> uidList = new ArrayList<>();
        sysDictTypeVoList.forEach(item -> {
            uidList.add(item.getUid());
        });
        // 判断要删除的分类
        QueryWrapper<SysDicData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.in("dict_type_uid", uidList);
        int count = sysDicDataService.count(queryWrapper);
        if (count > 0) {
            return "该分类下还有字典数据";
        }
        Collection<SysDicType> sysDicTypes = this.listByIds(uidList);
        sysDicTypes.forEach(item -> {
            item.setStatus(0);
            item.setUpdateTime(new Date());
        });
        // 数据库中更新
        this.updateBatchById(sysDicTypes);
        // 获取Redis中特定前缀
        return "删除成功";
    }
}
